import json
import random
import traceback

import requests
from django.contrib.auth.hashers import make_password, check_password
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from django_redis import get_redis_connection

from common.captcha.captcha import captcha
from .models import MXsold,MXwheel,MXtuijian,MXUser
from common.goodlist import goodslist,goodsindex
from common.get_cart_data import get_cart_data,get_all_num
from common.func import cookie_to_redis,is_login

# Create your views here.
def info(request):

    mxwheels = MXwheel.objects.all()
    mxsolds = MXsold.objects.all()
    mxtuijians = MXtuijian.objects.all()
    mxgoodlists = goodslist()
    mxindexs = goodsindex()
    cart_data = get_cart_data(request)
    all = get_all_num(request)

    context = {
        'mxwheels':mxwheels,
        'mxsolds':mxsolds,
        'mxtuijians':mxtuijians,
        'mxgoodlists':mxgoodlists,
        'mxindexs':list(mxindexs),
        'cart_data':cart_data,
        'all':all


    }

    return render(request,'index.html',context)


def register(request):
    if request.method == 'GET':
        return render(request,'register.html')
    elif request.method == 'POST':
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        pwd2 = request.POST.get('pwd2')
        imgcode = request.POST.get('imgcode')


        exists = MXUser.objects.filter(username=username).exists()
        if exists:
            return HttpResponse('该用户已注册')
        if pwd != pwd2:
            return HttpResponse('两次密码不一致')

        # 判断验证码是否过期及是否正确
        redis_cli = get_redis_connection()
        redis_code = redis_cli.get('imgcode-%s'%request.user)

        # print(redis_code)

        if not redis_code:
            return HttpResponse('验证码已过期')
        if redis_code.decode() != imgcode:
            return HttpResponse('验证码输入有误')


        # 信息无误后存入数据库
        MXUser.objects.create(
            username=username,
            pwd=make_password(pwd)
        )

        return redirect(reverse('index:login'))




def imagecode(request):
    name, text, bin_con = captcha.generate_captcha()
    redis_cli = get_redis_connection()
    redis_cli.set('imgcode-%s'%request.user,text,600)
    return HttpResponse(bin_con, content_type='image/jpg')


def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    elif request.method == 'POST':
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')

        try:
            user = MXUser.objects.get(username=username)
        except MXUser.DoesNotExist:
            return HttpResponse('用户未注册')

        if not check_password(pwd,user.pwd):
            return HttpResponse('密码错误')

        request.session['username'] = username

        res = redirect(reverse('index:info'))
        ret = cookie_to_redis(request,res)

        return ret


def logout(request):
    del request.session['username']

    return redirect(reverse('index:info'))
